package com.sys.basics.controller;

import com.sys.basics.config.annotation.SysLogAnnotation;
import com.sys.basics.entity.SysOrg;
import com.sys.basics.service.SysUserService;
import com.sys.basics.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import com.sys.basics.config.jsonRes.JsonResult;
import com.sys.basics.config.jsonRes.PageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sys.basics.entity.SysOrg;
import com.sys.basics.service.SysOrgService;
import io.swagger.annotations.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * @author: ares
 * @date: 2021-10-08
 * @description: 系统组织表 前端控制器
 */
@Validated
@Slf4j
@Api(tags = "组织管理")
@RestController
@RequestMapping("/basics/sysOrg")
public class SysOrgController extends AbstractController<SysOrgService, SysOrg> {

    private JsonResult<List<SysOrg>> listResult = new JsonResult<>();

    @Autowired
    SysOrgService sysOrgService;

    @SysLogAnnotation("根据id查询组织tree")
    @ApiOperation("根据id查询tree")
    @ApiImplicitParam(name = "id", value = "节点id", required = true, defaultValue = "1", paramType = "query")
    @GetMapping("/getTreeById")
    public JsonResult<SysOrg> getTreeById(@NotNull(message = "组织id不能为空") Long id) {
        return result.success("tree查询成功", sysOrgService.getTreeById(id));
    }

    @SysLogAnnotation("根据id查询组织tree(包含角色)")
    @ApiOperation("根据id查询tree(包含角色)")
    @ApiImplicitParam(name = "id", value = "节点id", required = true, defaultValue = "1", paramType = "query")
    @GetMapping("/getTreeByIdWithRole")
    public JsonResult<SysOrg> getTreeByIdWithRole(@NotNull(message = "组织id不能为空") Long id) {
        return result.success("tree(包含角色)查询成功", sysOrgService.getTreeByIdWithRole(id));
    }

    @SysLogAnnotation("根据父节点id查询组织tree")
    @ApiOperation("根据父节点id查询tree")
    @ApiImplicitParam(name = "parentId", value = "父节点id", required = true, defaultValue = "0", paramType = "query")
    @GetMapping("/getChildrenTreeByParentId")
    public JsonResult<List<SysOrg>> getChildrenTreeByParentId(@NotNull(message = "父组织id不能为空") Long parentId) {
        return listResult.success("tree查询成功", sysOrgService.getChildrenTreeByParentId(parentId));
    }

    @SysLogAnnotation("根据父节点id查询组织tree(包含角色)")
    @ApiOperation("根据父节点id查询tree(包含角色)")
    @ApiImplicitParam(name = "parentId", value = "父节点id", required = true, defaultValue = "0", paramType = "query")
    @GetMapping("/getChildrenTreeByParentIdWithRole")
    public JsonResult<List<SysOrg>> getChildrenTreeByParentIdWithRole(@NotNull(message = "父组织id不能为空") Long parentId) {
        return listResult.success("tree(包含角色)查询成功", sysOrgService.getChildrenTreeByParentIdWithRole(parentId));
    }

    @SysLogAnnotation("根据父节点id查询子组织")
    @ApiOperation("根据父节点id查询子组织")
    @ApiImplicitParam(name = "parentId", value = "父节点id", required = true, defaultValue = "0", paramType = "query")
    @GetMapping("/getChildrenByParentId")
    public JsonResult<List<SysOrg>> getChildrenByParentId(@NotNull(message = "父组织id不能为空") Long parentId) {
        return listResult.success("查询子组织成功", sysOrgService.getChildrenByParentId(parentId));
    }

    @SysLogAnnotation("根据父节点id查询子组织(包含角色)")
    @ApiOperation("根据父节点id查询子组织(包含角色)")
    @ApiImplicitParam(name = "parentId", value = "父节点id", required = true, defaultValue = "0", paramType = "query")
    @GetMapping("/getChildrenByParentIdWithRole")
    public JsonResult<List<SysOrg>> getChildrenByParentIdWithRole(@NotNull(message = "父组织id不能为空") Long parentId) {
        return listResult.success("查询子组织(包含角色)成功", sysOrgService.getChildrenByParentIdWithRole(parentId));
    }

    @SysLogAnnotation("根据组织实体条件查询组织列表")
    @ApiOperation("根据组织实体条件查询组织列表")
    @PostMapping("/getListByOrgEntity")
    public JsonResult<List<SysOrg>> getListByOrgEntity(@RequestBody SysOrg org) {
        return listResult.success("查询组织成功", sysOrgService.getListByOrgEntity(org));
    }

    @SysLogAnnotation("根据用户id查询关联的组织(包含角色信息)")
    @ApiOperation("根据用户id查询关联的组织(包含角色信息)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "currentOrgType", value = "当前登录用户当前组织类型", required = true, paramType = "query")
    })
    @GetMapping("/getListByUserIdWithRole")
    public JsonResult<List<SysOrg>> getListByUserIdWithRole(@NotNull(message = "用户id不能为空") Long userId,
                                                            @NotEmpty(message = "用户组织类型不能为空") String currentOrgType) {
        return listResult.success("查询关联的组织及角色成功", sysOrgService.getListByUserIdWithRole(userId, currentOrgType));
    }

    @SysLogAnnotation("根据子节点id查询上级组织tree")
    @ApiOperation("根据子节点id查询上级tree")
    @ApiImplicitParam(name = "id", value = "子节点的id", required = true, defaultValue = "6", paramType = "query")
    @GetMapping("/getTreeByIdAsLeaf")
    public JsonResult<SysOrg> getTreeByIdAsLeaf(@NotNull(message = "子组织id不能为空") Long id) {
        return result.success("tree查询成功", sysOrgService.getTreeByIdAsLeaf(id));
    }

    @SysLogAnnotation("根据id删除组织自身及子节点")
    @ApiOperation("根据id删除自身及子节点")
    @Override
    public JsonResult removeById(Long id) {
        boolean removed = sysOrgService.removeSelfAndChildrenById(id);
        if (removed) {
            result.success("删除成功");
        } else {
            result.error("删除失败");
        }
        return result;
    }

    @SysLogAnnotation("查询当前用户所有组织")
    @ApiOperation("查询当前用户所有组织")
    @GetMapping("/getCurrentUserOrgs")
    public JsonResult<List<SysOrg>> getCurrentUserOrgs() {
        return listResult.success("查询当前用户所有组织成功", sysOrgService.getCurrentUserOrgs(ShiroUtils.getUserId()));
    }

    @SysLogAnnotation("查询当前用户所有组织(包含角色)")
    @ApiOperation("查询当前用户所有组织(包含角色)")
    @GetMapping("/getCurrentUserOrgsWithRole")
    public JsonResult<List<SysOrg>> getCurrentUserOrgsWithRole() {
        return listResult.success("查询当前用户所有组织(包含角色)成功", sysOrgService.getCurrentUserOrgsWithRole(ShiroUtils.getUserId()));
    }

}
